﻿package ui.component
{
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;

	import util.GameTimer;

	/**
	 * 时间进度显示，显示格式如：10小时22分11秒(默认)，或10:22:11，或10-22-11，或10/22/11等。
	 * @author luguobin
	 */
	public class TimerLabel extends TextField
	{
		private var _formatString:String = "HH:MM:SS"; //文本字段中所显示时间的格式。
		private var _formatItems:Array = ["HH:MM:SS", "HH-MM-SS", "HH/MM/SS"]; //暂时格式提供这么多先。
		private var _formatType:int = -1; //默认是格式为：10小时22分11秒
		private var _endTimerHandle:Function; //时间跑完后，回调函数。
		private var _playingHandle:Function;
		private var _timer:GameTimer = new GameTimer(1000);
		private var _leaveTime:int = 0; //剩余时间，以"秒"计数。
		private var _totalTime:int = -1; //总的时间，如果是-1，即无限走下去。
		private var _goingType:int = 0; //时间走向类型。0：向下走时间，即时间越来越少。1：向上走时间，即时间越来越长。
		private var _hour:uint = 0;
		private var _minute:uint = 0;
		private var _second:uint = 0;
		///////////
		private var _prefixText:String = ''; //前缀文字
		private var _suffixText:String = ''; //后缀文字

		public function TimerLabel(endHandle:Function = null, playingHandle:Function = null, prefixText:String = '', suffixText:String = '')
		{
			_endTimerHandle = endHandle;
			_playingHandle = playingHandle;
			_prefixText = prefixText;
			_suffixText = suffixText;
			this.autoSize = TextFieldAutoSize.LEFT;
			this.selectable = false;
		}

		private function timerPlay(event:TimerEvent):void
		{
			_goingType == 0 ? _leaveTime-- : _leaveTime++;

			if(_leaveTime < 0 || (_totalTime != -1 && _leaveTime > _totalTime))
			{
				_timer.stop();
				_timer.removeEventListener(TimerEvent.TIMER, timerPlay);

				if(_endTimerHandle != null)
				{
					_endTimerHandle();
				}

				if(_leaveTime < 0)
				{
					_leaveTime = 0;
				}
			}
			else
			{
				this.htmlText = _prefixText + formatTime(_leaveTime) + _suffixText;

				if(_playingHandle != null)
				{
					_playingHandle();
				}
			}
		}

		//格式时间
		private function formatTime(seconds:int):String
		{
			_hour = int(seconds / 3600);
			_minute = int(seconds % 3600 / 60);
			_second = int(seconds % 3600 % 60);
			var str:String = "";

			if(_formatType == 0) //HH:MM:SS
			{
				str = _hour + ":" + _minute + ":" + _second;
			}
			else if(_formatType == 1) //HH-MM-SS
			{
				str = _hour + "-" + _minute + "-" + _second;
			}
			else if(_formatType == 2) //HH/MM/SS
			{
				str = _hour + "/" + _minute + "/" + _second;
			}
			else
			{
				if(_hour != 0)
				{
					str += _hour + "小时";
				}

				if(_minute != 0 || _hour != 0)
				{
					str += _minute + "分";
				}
				str += _second + "秒";
			}
			return str;
		}

		/**设置结束时间回调**/
		public function set endTimerHandle(callback:Function):void
		{
			_endTimerHandle = callback;
		}

		/**设置跑动过程中回调**/
		public function set playingHandle(callback:Function):void
		{
			_playingHandle = callback;
		}

		/**剩余时间，向下走时间(即--)**/
		public function set leaveTime(seconds:int):void
		{
			if(seconds >= 0)
			{
				_goingType = 0;

				if(seconds > 0 && !_timer.hasEventListener(TimerEvent.TIMER))
				{
					_timer.addEventListener(TimerEvent.TIMER, timerPlay);
				}
				_leaveTime = seconds;
				this.htmlText = _prefixText + formatTime(_leaveTime) + _suffixText;

				if(!_timer.running && seconds > 0)
				{
					_timer.start();
				}
			}
		}

		/**开始时间，向上走时间(即++)**/
		public function set startTime(seconds:int):void
		{
			if(seconds >= 0 && (_totalTime == -1 || seconds < _totalTime))
			{
				_goingType = 1;

				if(!_timer.hasEventListener(TimerEvent.TIMER))
				{
					_timer.addEventListener(TimerEvent.TIMER, timerPlay);
				}
				_leaveTime = seconds;
				this.htmlText = _prefixText + formatTime(_leaveTime) + _suffixText;

				if(!_timer.running)
				{
					_timer.start();
				}
			}
		}

		/**总时间，对"向上走时间"有影响**/
		public function set totalTime(seconds:int):void
		{
			_totalTime = seconds;
		}

		public function setTimerStop():void
		{
			_timer.stop();
			this.htmlText = _prefixText + formatTime(0) + _suffixText;
		}

		/**剩余时间**/
		public function get leaveTime():int
		{
			return _leaveTime;
		}

		/**时间显示格式方式**/
		public function get formatString():String
		{
			return _formatString;
		}

		/**
		 * 设置时间显示格式方式.
		 * 目前可以传入的字符串有这三种：'HH:MM:SS','HH-MM-SS','HH/MM/SS'
		 * 不设置该属性时，默认的是：10小时22分11秒-----这种格式。
		 * @param value
		 */
		public function set formatString(value:String):void
		{
			_formatType = _formatItems.indexOf(value);

			if(_formatType == -1)
			{
				return;
			}
			_formatString = value;
		}

		public function clear():void
		{
			if(_timer.running)
			{
				_timer.stop();
			}

			if(_timer.hasEventListener(TimerEvent.TIMER))
			{
				_timer.removeEventListener(TimerEvent.TIMER, timerPlay);
			}
			this.text = "";
		}

		public function get timer():GameTimer
		{
			return _timer;
		}

		/**前缀文字**/
		public function get prefixText():String
		{
			return _prefixText;
		}

		public function set prefixText(value:String):void
		{
			_prefixText = value;
		}

		/**后缀文字**/
		public function get suffixText():String
		{
			return _suffixText;
		}

		public function set suffixText(value:String):void
		{
			_suffixText = value;
		}
	}
}